package com.citrinus.feedme.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public final class DBHelper extends SQLiteOpenHelper {
	private static final String TAG = DBHelper.class.getSimpleName();

	public DBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_PLACES_SQL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, String.format("UPGRADING FROM %d to %d. CURRENT DATA IS ABOUT TO BE OVERWRITTEN.", oldVersion, newVersion));

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLACES);
		onCreate(db);
	}

	// ---------------------------------------------------------------------------------------------
	// -- RADARS
	// ---------------------------------------------------------------------------------------------

	public static final String TABLE_PLACES = "places";

	public static final String TABLE_PLACES_ID = "id";
	public static final String TABLE_PLACES_NAME = "name";
	public static final String TABLE_PLACES_REFERENCE= "reference";
	public static final String TABLE_PLACES_ICON = "icon";
	public static final String TABLE_PLACES_VICINITY = "vicinity";
	public static final String TABLE_PLACES_LATITUDE = "latitude";
	public static final String TABLE_PLACES_LONGITUDE = "longitude";

	private static final String CREATE_TABLE_PLACES_SQL = "CREATE TABLE " + TABLE_PLACES + " " +
			"(" + TABLE_PLACES_ID + " text PRIMARY KEY, "
			+ TABLE_PLACES_NAME + " text, " 
			+ TABLE_PLACES_REFERENCE + " TEXT, "
			+ TABLE_PLACES_ICON + " TEXT, "
			+ TABLE_PLACES_VICINITY + " TEXT, "
			+ TABLE_PLACES_LATITUDE + " REAL, " 
			+ TABLE_PLACES_LONGITUDE+ " REAL " 
			+");";
}
